﻿#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>

struct Product {
    int id;
    std::string name;
    double price;
    int sales;
    bool inStock;
};

struct User {
    std::vector<int> viewedProductIds;
};

void eCommerceDemo() {
    std::vector<Product> products = {
        {1, "Laptop", 999.99, 150, true},
        {2, "Phone", 699.99, 300, true},
        {3, "Tablet", 349.99, 80, false},
        {4, "Monitor", 249.99, 120, true},
        {5, "Keyboard", 49.99, 250, true}
    };

    User currentUser = { {1, 4, 5} };

    // 1. 过滤出有库存的商品
    products.erase(
        std::remove_if(products.begin(), products.end(),
            [](const Product& p) { return !p.inStock; }),
        products.end()
    );

    // 2. 按销量排序
    std::sort(products.begin(), products.end(),
        [](const Product& a, const Product& b) { return a.sales > b.sales; });

    // 3. 生成推荐列表（用户看过且库存中有的商品）
	std::vector<Product> recommendations;//推荐列表
    std::sort(currentUser.viewedProductIds.begin(), currentUser.viewedProductIds.end());

    for (int id : currentUser.viewedProductIds) {
        auto it = std::find_if(products.begin(), products.end(),
            [id](const Product& p) { return p.id == id; });
        if (it != products.end()) {
            recommendations.push_back(*it);
        }
    }

    // 4. 购物车功能，买的产品和数量
    std::vector<std::pair<Product, int>> cart = {
        {products[0], 1},  // 1 Laptop
        {products[3], 2}   // 2 Keyboards
    };

    double total = std::accumulate(cart.begin(), cart.end(), 0.0,
        [](double sum, const auto& item) {
            return sum + item.first.price * item.second;
        });

    // 打印结果
    std::cout << "Available products sorted by popularity:\n";
    for (const auto& product : products) {
        std::cout << product.name << " ($" << product.price
            << ", Sales: " << product.sales << ")\n";
    }

    std::cout << "\nRecommended for you:\n";
    for (const auto& product : recommendations) {
        std::cout << product.name << "\n";
    }

    std::cout << "\nCart total: $" << total << "\n";
}

int main() {
    eCommerceDemo();
    return 0;
}